package com.masterclient.app.modules.shop.contract;

import com.eoner.baselib.presenter.ThrowableConsumer;
import com.eoner.baselibrary.bean.personal.UploadImgBean;
import com.eoner.baselibrary.bean.shop.DealerStoreInfoBean;
import com.eoner.baselib.utils.toast.ToastUtil;
import com.eoner.baselib.container.CommonBaseResponse;
import com.masterclient.app.httpservice.RetrofitUtil;
import com.masterclient.app.httpservice.RetrofitUtil2;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;

import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * @auther hjian
 * @date 2020/9/11.
 */
public class DealerStoreInfoPresenter extends DealerStoreInfoContract.Presenter {
    @Override
    public void getStoreInfo() {
        mRxManager.add(toSubscribe(RetrofitUtil2.getInstance().dealerStoreInfo(), new Consumer<CommonBaseResponse<DealerStoreInfoBean>>() {
            @Override
            public void accept(@NonNull CommonBaseResponse<DealerStoreInfoBean> bean) throws Exception {
                if(bean.getCode() == 0){
                    mView.showStoreInfo(bean.getData());
                }else {
                    ToastUtil.showToast(bean.getMsg());
                }
            }
        }, new ThrowableConsumer(){
            @Override
            public void accept(Throwable o) throws Exception {
                super.accept(o);
                ToastUtil.showToast("网络异常，请重试");
            }
        }));
    }

    @Override
    public void updateInfo(String logo, String name, String use_default) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("logo",logo);
            jsonObject.put("name",name);
            jsonObject.put("use_default",use_default);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),
                jsonObject.toString());
        mRxManager.add(toSubscribe(RetrofitUtil2.getInstance().updateDealerInfo(body), new Consumer<CommonBaseResponse>() {
            @Override
            public void accept(@NonNull CommonBaseResponse bean) throws Exception {
                if(bean.getCode() == 0){
                    mView.updateResult(true);
                }else {
                    mView.updateResult(false);
                    ToastUtil.showToast(bean.getMsg());
                }
            }
        }, new ThrowableConsumer(){
            @Override
            public void accept(Throwable o) throws Exception {
                super.accept(o);
                mView.updateResult(false);
                ToastUtil.showToast("网络异常，请重试");
            }
        }));
    }

    @Override
    public void uploadImage(File image) {
        MultipartBody.Builder builder = new MultipartBody.Builder()
                .setType(MultipartBody.FORM);

        RequestBody imageBody = RequestBody.create(MediaType.parse("image/png"), image);
        builder.addFormDataPart("imgfile", "123.jpeg", imageBody);
        builder.addFormDataPart("type","customer");
        mRxManager.add(toSubscribe(RetrofitUtil.getInstance().uploadImage(builder.build().parts()), new Consumer<UploadImgBean>() {
            @Override
            public void accept(@NonNull UploadImgBean bean) throws Exception {
                if("0".equals(bean.getCode())){
                    mView.uploadImgResult(true,bean.getData());
                }else {
                    ToastUtil.showToast(bean.getMsg());
                    mView.uploadImgResult(false,null);
                }
            }
        }, new ThrowableConsumer(){
            @Override
            public void accept(Throwable o) throws Exception {
                super.accept(o);
                ToastUtil.showToast("网络异常，请重试");
                mView.uploadImgResult(false, null);
            }
        }));
    }
}
